package com.itheima.controller.sms;

import com.itheima.api.sms.SmsFlashPromotionSessionService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.dto.SmsFlashPromotionSessionDto;
import com.itheima.vo.pojo.SmsFlashPromotionSession;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("flashSession") //这个实现类是秒杀活动列表里面的秒杀时间段列表
public class SmsFlashPromotionSessionController {

    @DubboReference
    private SmsFlashPromotionSessionService smsFlashPromotionSessionService;

    //获取全部场次
    @GetMapping("list")
    public AjaxResult list() {

        List<SmsFlashPromotionSession> list = smsFlashPromotionSessionService.list();

        return AjaxResult.success(list);
    }

    //添加场次
    @PostMapping("create")
    public AjaxResult create(@RequestBody SmsFlashPromotionSession smsFlashPromotionSession) {

        //自动获取当前时间
        smsFlashPromotionSession.setCreateTime(LocalDateTime.now());
        smsFlashPromotionSessionService.save(smsFlashPromotionSession);

        return AjaxResult.success("添加成功");
    }

    //删除场次
    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id) {

        smsFlashPromotionSessionService.removeById(id);

        return AjaxResult.success("删除成功");
    }

    //获取场次详情
    @GetMapping("{id}")
    public AjaxResult findById(@PathVariable Long id) {

        SmsFlashPromotionSession sps = smsFlashPromotionSessionService.getById(id);

        return AjaxResult.success(sps);
    }

    //修改场次
    @PostMapping("/update/{id}")
    public AjaxResult update(@PathVariable Long id, @RequestBody SmsFlashPromotionSession smsFlashPromotionSession) {

        smsFlashPromotionSession.setId(id);
        smsFlashPromotionSessionService.updateById(smsFlashPromotionSession);

        return AjaxResult.success("修改成功");
    }

    //修改启用状态
    @PostMapping("/update/status/{id}")
    public AjaxResult updateStatus(@PathVariable Long id, Long status) {
        SmsFlashPromotionSession smsFlashPromotionSession = new SmsFlashPromotionSession();
        smsFlashPromotionSession.setId(id);
        smsFlashPromotionSession.setStatus(status);

        smsFlashPromotionSessionService.updateStatus(smsFlashPromotionSession);

        return AjaxResult.success("修改状态成功");
    }

    //获取全部可选场次及其数量(用在秒杀活动列表里的设置商品)
    @GetMapping("selectList")
    public AjaxResult selectList(Long flashPromotionId) {
        List<SmsFlashPromotionSessionDto> list = smsFlashPromotionSessionService.selectList(flashPromotionId);

        return AjaxResult.success(list);
    }
}
